#include "../userprog/syscall.h"


/*/======== Manager Function ========*/
void main()
{
	int myIndex;
	int loops;
	int activeTicketTakers;
	int activeTicketTakers1;
	int tcRevenue;
	int concRevenue;
	int nextTcRevenue;
	int nextConcRevenue;
	
	int longestLineLength;
	int longestLineIndex;
	
	int shortLineIndex;
	
	/* Counters */
	int i0;
	int i1;
	int i2;
	int i3;
	int i4;
	int i5;
	int i6;
	int i7;
	int i8;
	int i9;
	int i10;
	int i11;
	int i12;
	int i13;
	
	int i14;
	int i15;
	int i16;
	
	int j0;
	int j1;
	int j2;
	int j3;
	int j4;
	
	int k0;
	int k1;
	int k2;
	
	/*Shared Data*/
	int PRINT_DEBUG_MESSAGES;
	int PRINT_OUTPUT_MESSAGES;
	int numCustomersInTheaterLock;
	int MOVIE_STATE;
	int ROOM_EMPTY, ACCEPTING_TICKETS, CUSTOMER_CUTOFF, IN_PROGRESS, ROOM_EMPTYING;
	int movieStateLock;
	
	int NUM_TICKET_CLERKS;
int NUM_CONCESSION_CLERKS;
int NUM_TICKET_TAKERS;
	
	
	
	int MANAGER_DELAY_ACCEPTING;
int MANAGER_ROLL_ACCEPT;
int MANAGER_YIELD_ACCEPTING;
int MANAGER_DELAY_CUTOFF;
int MANAGER_DELAY_PROFITS;
int MANAGER_DELAY_BREAK;
int MANAGER_ROLL_ONBREAK;
int MANAGER_LONGLINE;
int MANAGER_SHORTLINE;

int profitLock;
int movieTechLock;
int movieTechCondition;
int tcsOnBreak;
int ccsOnBreak;
int ttsOnBreak;
int beginTakingTicketsLock;
int beginTakingTicketsCondition;
int tcBreakLock;
int tcBreakCondition;
int tcBreakStopLock;
int tcBreakStopCondition;
int ccBreakLock;
int ccBreakCondition;
int ccBreakStopLock;
int ccBreakStopCondition;
int ttBreakLock;
int ttBreakCondition;
int ttBreakStopLock;
int ttBreakStopCondition;

int ttInteractionLock;
int ttLobbyLock;
int ttLobbyCondition;
int ttState;
int CLERK_ON_BREAK;
int ticketTakerFull;
int ttLineManager;
int CLERK_AVAILABLE;
int ttForcedToStop;
int ttInteractionCondition;
int ttStateLock;
int ttStopLock;
int ttStopCondition;
int ttLineCondition;
int ttLineLength;
int totalTicketsSold;
int totalPopcornSold;
int totalSodaSold;
int numCustomersInTheater;
int SODA_PRICE;
int POPCORN_PRICE;
int TICKET_PRICE;
	
	/*Acquire Shared Data*/
	NUM_TICKET_CLERKS = CreateMV(1, "NUM_TICKET_CLERKS", 17);
	NUM_CONCESSION_CLERKS = CreateMV(1, "NUM_CONCESSION_CLERKS", 21);
	NUM_TICKET_TAKERS = CreateMV(1, "NUM_TICKET_TAKERS", 17);
	
	PRINT_DEBUG_MESSAGES = CreateMV(1,"PRINT_DEBUG_MESSAGES",20);
	PRINT_OUTPUT_MESSAGES = CreateMV(1,"PRINT_OUTPUT_MESSAGES", 21);
	numCustomersInTheaterLock = CreateLock("numCustomersInTheaterLock",25);
	MOVIE_STATE = CreateMV(1,"MOVIE_STATE",11);
	ROOM_EMPTY = CreateMV(1,"ROOM_EMPTY",10);
	ACCEPTING_TICKETS = CreateMV(1,"ACCEPTING_TICKETS",17);
	CUSTOMER_CUTOFF = CreateMV(1,"CUSTOMER_CUTOFF",15);
	IN_PROGRESS = CreateMV(1,"IN_PROGRESS",11);
	ROOM_EMPTYING = CreateMV(1,"ROOM_EMPTYING",13);
	movieStateLock = CreateLock("movieStateLock",14);
	MANAGER_DELAY_ACCEPTING = CreateMV(1, "MANAGER_DELAY_ACCEPTING", 23);
	MANAGER_ROLL_ACCEPT = CreateMV(1, "MANAGER_ROLL_ACCEPT", 19);
	MANAGER_YIELD_ACCEPTING = CreateMV(1,"MANAGER_YIELD_ACCEPTING", 23);
	MANAGER_DELAY_CUTOFF = CreateMV(1, "MANAGER_DELAY_CUTOFF",20);
	MANAGER_DELAY_PROFITS = CreateMV(1,"MANAGER_DELAY_PROFITS",21);
	MANAGER_DELAY_BREAK = CreateMV(1,"MANAGER_DELAY_BREAK",19);
	MANAGER_ROLL_ONBREAK = CreateMV(1,"MANAGER_ROLL_ONBREAK", 20);
	MANAGER_LONGLINE = CreateMV(1,"MANAGER_LONGLINE",16);
	MANAGER_SHORTLINE = CreateMV(1,"MANAGER_SHORTLINE",17);
	profitLock = CreateLock("profitLock",10);
	movieTechLock = CreateLock("movieTechLock",13);
	movieTechCondition = CreateCondition("movieTechCondition",18);
	tcsOnBreak = CreateMV(1,"tcsOnBreak",10);
	ccsOnBreak = CreateMV(1,"ccsOnBreak",10);
	ttsOnBreak = CreateMV(1,"ttsOnBreak",10);
	beginTakingTicketsLock = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"beginTakingTicketsLock",22);
	beginTakingTicketsCondition = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"beginTakingTicketsCondition",27);
	tcBreakLock = CreateMV(GetMV(NUM_TICKET_CLERKS,0),"tcBreakLock",11);
	tcBreakCondition = CreateMV(GetMV(NUM_TICKET_CLERKS,0),"tcBreakCondition",16);
	tcBreakStopLock = CreateMV(GetMV(NUM_TICKET_CLERKS,0),"tcBreakStopLock",15);
	tcBreakStopCondition = CreateMV(GetMV(NUM_TICKET_CLERKS,0),"tcBreakStopCondition",20);
	ccBreakLock = CreateMV(GetMV(NUM_CONCESSION_CLERKS,0),"ccBreakLock",11);
	ccBreakCondition = CreateMV(GetMV(NUM_CONCESSION_CLERKS,0),"ccBreakCondition",16);
	ccBreakStopLock = CreateMV(GetMV(NUM_CONCESSION_CLERKS,0),"ccBreakStopLock",15);
	ccBreakStopCondition = CreateMV(GetMV(NUM_CONCESSION_CLERKS,0),"ccBreakStopCondition",20);
	ttBreakLock = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttBreakLock",11);
	ttBreakCondition = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttBreakCondition",16);
	ttBreakStopLock = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttBreakStopLock",15);
	ttBreakStopCondition = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttBreakStopCondition",20);	
	ttInteractionLock = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttInteractionLock",17);
	ttLobbyLock = CreateMV(GetMV(NUM_TICKET_TAKERS,0)+1,"ttLobbyLock",11);
	ttLobbyCondition = CreateMV(GetMV(NUM_TICKET_TAKERS,0)+1,"ttLobbyCondition",16);
	ttState = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttState",7);
	CLERK_ON_BREAK = CreateMV(1,"CLERK_ON_BREAK",14);
	ticketTakerFull = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ticketTakerFull",15);
	ttLineManager = CreateLock("ttLineManager",13);
	CLERK_AVAILABLE = CreateMV(1,"CLERK_AVAILABLE",15);
	ttForcedToStop = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttForcedToStop",14);
	ttInteractionCondition = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttInteractionCondition",22);
	ttStateLock = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttStateLock",11);
	ttStopLock = CreateLock("ttStopLock",10);
	ttStopCondition = CreateCondition("ttStopCondition",15);
	ttLineCondition = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttLineCondition",15);
	ttLineLength = CreateMV(GetMV(NUM_TICKET_TAKERS,0),"ttLineLength",12);
	totalTicketsSold = CreateMV(GetMV(NUM_TICKET_CLERKS,0),"totalTicketsSold",16);
	totalPopcornSold = CreateMV(GetMV(NUM_CONCESSION_CLERKS,0),"totalPopcornSold",16);
	totalSodaSold = CreateMV(GetMV(NUM_CONCESSION_CLERKS,0),"totalSodaSold",13);
	numCustomersInTheater = CreateMV(1,"numCustomersInTheater",21);
	SODA_PRICE = CreateMV(1,"SODA_PRICE", 10);
	POPCORN_PRICE = CreateMV(1,"POPCORN_PRICE", 13);	
	TICKET_PRICE = CreateMV(1,"TICKET_PRICE", 12);
	
	myIndex = 0;
	
	if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager created\n", 17, 0, 0);
	AcquireLock(numCustomersInTheaterLock);
	
	loops = 0;
	
	do
	{
		
		ReleaseLock(numCustomersInTheaterLock);
		for(i0 = 0; i0 < 10; i0++){
		Yield();
		}
		if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Loop #%d\n", 19, 1, loops);
		loops++;
		if(GetMV(PRINT_DEBUG_MESSAGES,0) && GetMV(MOVIE_STATE,0) == GetMV(ROOM_EMPTY,0)) FPrint("Manager: Movie state is ROOM_EMPTY\n", 36, 0, 0);
		if(GetMV(PRINT_DEBUG_MESSAGES,0) && GetMV(MOVIE_STATE,0) == GetMV(ACCEPTING_TICKETS,0)) FPrint("Manager: Movie state is ACCEPTING_TICKETS\n", 43, 0, 0);
		if(GetMV(PRINT_DEBUG_MESSAGES,0) && GetMV(MOVIE_STATE,0) == GetMV(CUSTOMER_CUTOFF,0)) FPrint("Manager: Movie state is CUSTOMER_CUTOFF\n", 41, 0, 0);
		if(GetMV(PRINT_DEBUG_MESSAGES,0) && GetMV(MOVIE_STATE,0) == GetMV(IN_PROGRESS,0)) FPrint("Manager: Movie state is IN_PROGRESS\n", 37, 0, 0);
		if(GetMV(PRINT_DEBUG_MESSAGES,0) && GetMV(MOVIE_STATE,0) == GetMV(ROOM_EMPTYING,0)) FPrint("Manager: Movie state is ROOM_EMPTYING\n", 39, 0, 0);
		/*
	
		
	
		STATES
	
		ROOM_EMPTY: movie has ended, screening room is empty (initial state, also set by movie tech). manager can randomly decide to start next movie.
		when he does, he activates the takers, announces to the lobby and changes the state to...

		ACCEPTING_TICKETS: customers are handing tickets to ticket takers. this goes on until all ticket takers are full. ticket takers can also be randomly
		set to full by the manager when he feels like it. whenever a taker is full, his line is disbanded. when manager detects that all ticket
		takers are full he changes the state to:

		CUSTOMER_CUTOFF: ticket takers are deactivated, customers are filling the seats. this goes on until the number in the ticket countbox matches the
		number of seated people in the theater. when this happens the movie tech changes state and yields for a while

		IN_PROGRESS: movie tech is yielding. when the yield ends, first thing he does is change the state to...

		ROOM_EMPTYING: movie tech busy loops waiting for the number of seated people to hit zero. when this occurs he changes the state back to BEGIN
	
	
	
		
	
		//if in ROOM_EMPTY state and loop multiple of XXXX
		//roll to see if you want to start taking customers. if so:
		//activate ticket takers. wait until they have all been successfully activated
		//broadcast to the lobby so customers bum-rush the ticket takers
		//set state to ACCEPTING_TICKETS, yield certain number of times (to prevent accepting state from not lasting long enough)
		
		*/
		if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Acquiring movieStateLock\n", 35, 0, 0);
		AcquireLock(movieStateLock);
		if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Acquired movieStateLock\n", 34, 0, 0);
		if((GetMV(MOVIE_STATE,0) == ROOM_EMPTY) && ((loops % MANAGER_DELAY_ACCEPTING) == 0))
		{
			if((RandInt() % 101) <= GetMV(MANAGER_ROLL_ACCEPT,0))
			{
				if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager has decided to start the movie process!\n", 49, 0, 0);
				
				for(i1 = 0; i1 < NUM_TICKET_TAKERS; i1++)
				{
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Activating Ticket Taker %d \n", 38, 1, i1);
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: acquiring beginTakingTickets\n", 39, 0, 0);
					AcquireLock(GetMV(beginTakingTicketsLock,i1));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Signalling beginTakingTickets\n", 40, 0, 0);
					SignalCondition(GetMV(beginTakingTicketsCondition,i1), GetMV(beginTakingTicketsLock,i1));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Waiting beginTakingTickets %d\n", 40, 1, i1);
					WaitCondition(GetMV(beginTakingTicketsCondition,i1), GetMV(beginTakingTicketsLock,i1));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Releasing beginTakingTicketsLock[%d]\n", 47, 1, i1);
					ReleaseLock(GetMV(beginTakingTicketsLock,i1));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Acquiring ttInteractionLock[%d]\n", 42, 1, i1);
					AcquireLock(GetMV(ttInteractionLock,i1));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Releasing ttInteractionLock[%d]\n", 42, 1, i1);
					ReleaseLock(GetMV(ttInteractionLock,i1));
				}
				/*
				for(int i = 0; i < NUM_TICKET_TAKERS; i++)
				{
					if(ttState[i] != CLERK_ON_BREAK && ticketTakerFull[i])
					{
						if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager acquiring beginTakingTicketsLock\n");
						beginTakingTicketsLock -> Acquire();
						//Signal to begin taking tickets
						if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager signaling beginTakingTicketsLock\n");
						beginTakingTicketsCondition -> Signal(beginTakingTicketsLock);
						//Wait for confirmation
						if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager waiting beginTakingTicketsLock\n");
						beginTakingTicketsCondition -> Wait(beginTakingTicketsLock);
						beginTakingTicketsLock -> Release();
					}
				
				
				
				}
				*/
				
				/*beginTakingTicketsLock -> Acquire();			
				beginTakingTicketsCondition -> Broadcast(beginTakingTicketsLock);
				beginTakingTicketsLock -> Release();
			
				int readyTicketTakers;
				do
				{
					readyTicketTakers = 0;
					int i = 0;
					for(i; i < NUM_TICKET_TAKERS; i++)
					{
						ticketTakerFullLock -> Acquire();
						if(!ticketTakerFull[i])
						{
							readyTicketTakers++;
						}
						ticketTakerFullLock -> Release();
					}
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: %d out of %d Ticket Takers are ready to take tickets.\n", readyTicketTakers, NUM_TICKET_TAKERS);
				}
				while(readyTicketTakers < NUM_TICKET_TAKERS);
				*/
				if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: All Ticket Takers are ready to take tickets.\n", 55, 0, 0);
				
				/*/Note: this for loop goes to NUM_TICKET_TAKERS + 1 to account for the extra lobby for Customers who found no TicketTakers*/
				for(i2 = 0; i2 < GetMV(NUM_TICKET_TAKERS,0) + 1; i2++)
				{
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Acquiring ttLobbyLock %d\n", 35, 1, i2);
					AcquireLock(GetMV(ttLobbyLock,i2));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Broadcasting to ttLobbyLock %d\n", 41, 1, i2);
					BroadcastCondition(GetMV(ttLobbyCondition,i2), GetMV(ttLobbyLock,i2));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Releasing ttLobbyLock %d\n", 35, 1, i2);
					ReleaseLock(GetMV(ttLobbyLock,i2));
				}
				
				SetMV(MOVIE_STATE,0, GetMV(ACCEPTING_TICKETS,0));
				loops = 0;
				
				for(i3 = 0; i3 < GetMV(MANAGER_YIELD_ACCEPTING,0); i3++)
				{
					Yield();
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Yield #%d\n", 20, 1, i3);
				}
			
			}
			else
			{
				if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager has decided to NOT start the movie process right now!\n", 63, 0, 0);
			}
		}
		/*
		//if in ACCEPTING_TICKETS state (any loop multiple, but this is implicit)
		//iterate through fullness of ticket takers
		//if it's full, disband their line (do we need a local variable to track that we've already disbanded it to prevent doing it again?)
		//check if in loop multiple of XXXXX
		//if so, force them all to be full
		//(any loop) if there are no active ticket takers (full and break are inactive), tell movie tech to start movie. change state to CUSTOMER_CUTOFF (and reset the local array of inteans for line disbands)
		*/
		if(GetMV(MOVIE_STATE,0) == GetMV(ACCEPTING_TICKETS,0))
		{
			/*
			//if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Acquiring ticketTakerFullLock\n");
			//ticketTakerFullLock -> Acquire();
			*/
			/*
			for(int i = 0; i < NUM_TICKET_TAKERS; i++)
			{
			if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Acquiring ttStateLock[%d]\n", i);
			ttStateLock[i] -> Acquire();
			}
			*/
			
			activeTicketTakers = 0;
			/*
			//maybe force ticket takers to be full
			//for all full ticket takers, disband their lines
			*/
			for(i4 = 0; i4 < GetMV(NUM_TICKET_TAKERS,0); i4++)
			{
				if((loops % GetMV(MANAGER_DELAY_CUTOFF,0) == 0) && (GetMV(ttState,i4) != GetMV(CLERK_ON_BREAK,0)) && (!GetMV(ticketTakerFull,i4)))
				{
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Forcing TicketTaker %d to stop taking tickets\n", 56, 1, i4);
					/*/force this active clerk to be full.*/
					
					AcquireLock(ttLineManager);
					if(GetMV(ttState,i4) == GetMV(CLERK_AVAILABLE,0))
					{
						SetMV(ttForcedToStop,i4, 1);
						if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Acquiring ttInteractionLock[%d]\n", 42, 1, i4);
						AcquireLock(GetMV(ttInteractionLock,i4));
						SignalCondition(GetMV(ttInteractionCondition,i4), GetMV(ttInteractionLock,i4));
						WaitCondition(GetMV(ttInteractionCondition,i4), GetMV(ttInteractionLock,i4));
						if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Releasing ttInteractionLock[%d]\n", 42, 1, i4);
						ReleaseLock(GetMV(ttInteractionLock,i4));
						AcquireLock(GetMV(beginTakingTicketsLock,i4));
						ReleaseLock(GetMV(beginTakingTicketsLock,i4));
						ReleaseLock(ttLineManager);
					}
					else
					{
					ReleaseLock(ttLineManager);
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Acquiring ttStateLock %d\n", 35, 1, i4);
					AcquireLock(GetMV(ttStateLock,i4));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Setting TicketTaker %d's global stop intean to 1\n", 63, 1, i4);
					SetMV(ttForcedToStop,i4, 1);
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Acquiring ttStopLock\n", 31, 0, 0);
					AcquireLock(ttStopLock);
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Releasing ttStateLock %d\n", 35, 1, i4);
					ReleaseLock(GetMV(ttStateLock,i4));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Waiting for ttStopCondition on ttStopLock\n", 52, 0, 0);
					WaitCondition(ttStopCondition, ttStopLock);
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Signalling ttStopCondition on ttStopLock\n", 51, 0, 0);
					SignalCondition(ttStopCondition, ttStopLock);
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Releasing ttStopLock\n", 31, 0, 0);
					ReleaseLock(ttStopLock);
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Acquiring beginTakingTickstLock %d\n", 45, 1, i4);
					AcquireLock(GetMV(beginTakingTicketsLock,i4));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Releasing beginTakingTicketsLock %d\n", 46, 1, i4);
					ReleaseLock(GetMV(beginTakingTicketsLock,i4));
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: TicketTaker %d has been forced to be stop taking tickets\n", 67, 1, i4);
					/*/at this point, the tickettaker has executed beginTakingTicketsCondition -> Wait(beginTakingTicketsLock)*/
					
					
					
					/*
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Forcing Ticket Taker %d to be FULL\n", i);
					ticketTakerFull[i] = 1;*/
					}
				}
			
				if(GetMV(ticketTakerFull,i4) || GetMV(ttForcedToStop,i4))
				{
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Ticket Taker %d is FULL\n", 34, 1, i4);
					/*/disband their line*/
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Acquiring ttLineManager\n", 34, 0, 0);
					AcquireLock(ttLineManager);
					BroadcastCondition(GetMV(ttLineCondition,i4), ttLineManager);
					SetMV(ttLineLength,i4, 0);
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Releasing ttLineManager\n", 34, 0, 0);
					ReleaseLock(ttLineManager);
				}
				else if(GetMV(ttState,i4) != GetMV(CLERK_ON_BREAK,0)) /*/ not full, not on break == active*/
				{
					activeTicketTakers++;
				}
			}
			
			/*
			for(int i = 0; i < NUM_TICKET_TAKERS; i++)
			{
			if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Releasing ttStateLock[%d]\n", i);
			ttStateLock[i] -> Release();
			}
			*/
			if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Active ticket takers: %d\n", 35, 1, activeTicketTakers);
			/*
			//if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Releasing ticketTakerFullLock\n");
			//ticketTakerFullLock -> Release();
			
			//if all ticket takers are full its time to go to next phase
			
			*/
			
			if(activeTicketTakers == 0)
			{
			/*
			//make sure all ticket takers are not dealing with customers
			//signal movie tech
				//if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Acquiring ticketTakerFullLock\n");
				//ticketTakerFullLock -> Acquire();
				
				/*
				for(int i = 0; i < NUM_TICKET_TAKERS; i++)
				{
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Acquiring ttStateLock[%d]\n", i);
					ttStateLock[i] -> Acquire();
				}
				*/
				if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: acquiring movieTechLock\n", 34, 0, 0);
				AcquireLock(movieTechLock);
				SetMV(MOVIE_STATE, 0, GetMV(CUSTOMER_CUTOFF,0));
				if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Signalling MovieTechnician to start movie when all Customers are seated.\n", 83, 0, 0);
				if(GetMV(PRINT_OUTPUT_MESSAGES,0)) FPrint("Manager is telling the MovieTechnnician to start the movie.\n", 61, 0, 0);
				SignalCondition(movieTechCondition, movieTechLock);
				if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Releasing movieTechLock\n", 34, 0, 0);
				ReleaseLock(movieTechLock);
				
				/*
				for(int i = 0; i < NUM_TICKET_TAKERS; i++)
				{
					if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Releasing ttStateLock[%d]\n", i);
					ttStateLock[i] -> Release();
				}*//*
				//if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Releasing ticketTakerFullLock\n");
				//ticketTakerFullLock -> Release();*/
			}
			/*
			//if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Releasing ticketTakerFullLock\n");
			//ticketTakerFullLock -> Release();*/
		}
		
		
		ReleaseLock(movieStateLock);
		/*
		//if loop multiple of XXXX
		//process profits
		*/
		if((loops % GetMV(MANAGER_DELAY_PROFITS,0)) == 0)
		{
			if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Processing Profits\n", 29, 0, 0);
			if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Acquiring profitLock\n", 31, 0, 0);
			AcquireLock(profitLock);
			
			tcRevenue = 0;
			concRevenue = 0;
			
			for(i5 = 0; i5 < GetMV(NUM_TICKET_CLERKS,0); i5++)
			{
				if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: totalTicketsSold[%d] = %d\n", 36, 2, i5*100 + GetMV(totalTicketsSold,i5));
				nextTcRevenue = GetMV(totalTicketsSold,i5) * GetMV(TICKET_PRICE,0);
				tcRevenue += nextTcRevenue;
				if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: tcRevenue = $%d%d\n", 28, 2, tcRevenue);
				
				if(GetMV(PRINT_OUTPUT_MESSAGES,0)) FPrint("Manager collected $%d%d from TicketClerk %d.\n", 46, 3, nextTcRevenue*100 + i5);
			}
			
			for(i6 = 0; i6 < GetMV(NUM_CONCESSION_CLERKS,0); i6++)
			{
				nextConcRevenue = 0;
				nextConcRevenue += GetMV(totalPopcornSold,i6) * POPCORN_PRICE;
				nextConcRevenue += GetMV(totalSodaSold,i6) * SODA_PRICE;
				concRevenue += nextConcRevenue;
				if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: concRevenue = $%d%d\n", 30, 2, concRevenue);
				
				if(GetMV(PRINT_OUTPUT_MESSAGES,0)) FPrint("Manager collected $%d%d from ConcessionClerk %d.\n", 50, 3, nextConcRevenue*100 + i6);
			}
			
			if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Ticket Clerk Revenue: $%d%d\n", 38, 2, tcRevenue);
			if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Concessions Revenue: $%d%d\n", 37, 2, concRevenue);
			if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Total Revenue $%d%d\n", 30, 2, tcRevenue + concRevenue);
			
			if(GetMV(PRINT_OUTPUT_MESSAGES,0)) FPrint("Manager total money made by theater = %d%d\n", 44, 2, tcRevenue + concRevenue);			
			
			if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: Releasing profitLock\n", 31, 0, 0);
			ReleaseLock(profitLock);
		}
		
		/*
		          BREAK STUFF. ACQUIRE ALL STATE LOCKS
		if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Acquiring all state locks for break processing\n");
		for(int i = 0; i < NUM_TICKET_CLERKS; i++)
		{
			tcStateLock[i] -> Acquire();
		}
		
		for(int i = 0; i < NUM_CONCESSION_CLERKS; i++)
		{
			ccStateLock[i] -> Acquire();
		}
		
		for(int i = 0; i < NUM_TICKET_TAKERS; i++)
		{
			ttStateLock[i] -> Acquire();
		}
		if(GetMV(PRINT_DEBUG_MESSAGES,0)) printf("Manager: Finished acquiring all state locks\n");
		*/
		
		
		/*
		//on any loop
		//process taking people off break
		*/
		
		
		/*Breaks disabled, section has been deleted*/
		
	
	
	
		managerEnd:
		AcquireLock(numCustomersInTheaterLock);
		if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("Manager: There are %d customers remaining in the theater.\n", 59, 1, numCustomersInTheater);
	}
	while(numCustomersInTheater > 0);
	ReleaseLock(numCustomersInTheaterLock);
	

	
	
	
	if(GetMV(PRINT_OUTPUT_MESSAGES,0)) FPrint("No more customers in theater. Manager exiting.\n", 48, 0, 0);
	if(GetMV(PRINT_DEBUG_MESSAGES,0)) FPrint("No more customers in theater. Manager exiting.\n", 48, 0, 0);
	
	
	
	
	Exit(0);
	/*
	//maybe destroy all the locks here?
	//might be better to have the OS kill the locks cuz when the manager dies, other threads might still own locks
	//or does it not matter, since the program is ending anyway?*/
}